﻿<?php
	@ob_start();
	@require_once("DatabaseExec.php");
	
	$sType = $_GET['stype'];
	$queryString = $_GET['queryString'];
		
	switch($sType)
	{
		case 'province' : SuggestStartWith($sType,$queryString); break;
		case 'detailStatus' : SuggestLike($sType,$queryString); break;
		
		case 'decipler' : SuggestDecipler($queryString); break;
		case 'church' : SuggestFromRowInDB($queryString,$sType,'userdetail'); break;
		case 'job' : SuggestFromRowInDB($queryString,$sType,'userdetail'); break;
		case 'workplace' : SuggestFromRowInDB($queryString,$sType,'userdetail'); break;
		case 'cellGroup' : SuggestFromRowInDB($queryString,$sType,'userdetail'); break;
		case 'university' : SuggestUniversity($queryString); break;
		default: break;
	}
	
	function CallSuggestion($sql)
	{
		global $queryString;
		$rows = ExecSQLToArray($sql);
		
		@ob_end_clean();
		
		echo '<ul>';
		for($i=0;$i<count($rows);$i++)
		{
			$item = $rows[$i][0];
			echo '<li onClick="fill(\''.addslashes($item).'\');">'.$item.'</li>';
		}
		if(count($rows) == 0) echo "<li>$queryString</li>";
		echo '</ul>';
	}
	
	function SuggestStartWith($sType,$queryString)
	{
		$queryString = mysql_real_escape_string($queryString);
		$sql = "SELECT DISTINCT listData FROM `listdata` WHERE listType='$sType' AND listData LIKE '$queryString%' LIMIT 10";
		
		CallSuggestion($sql);
	}
	
	function SuggestLike($sType,$queryString)
	{
		$queryString = mysql_real_escape_string($queryString);
		$sql = "SELECT DISTINCT listData FROM `listdata` WHERE listType='$sType' AND listData LIKE '%$queryString%' LIMIT 10";
		
		CallSuggestion($sql);
	}
	
	function SuggestFromRowInDB($queryString,$rowName,$tableName)
	{
		$queryString = mysql_real_escape_string($queryString);
		$sql = "SELECT DISTINCT $rowName FROM $tableName WHERE $rowName LIKE '$queryString%' LIMIT 10";
		
		CallSuggestion($sql);
	}
	
	function SuggestDecipler($queryString)
	{
		$queryString = mysql_real_escape_string($queryString);
		$sql = "SELECT DISTINCT `name`,lastname FROM userinfo WHERE `name` LIKE '$queryString%' OR lastname LIKE '$queryString%' LIMIT 10";
		
		$rows = ExecSQLToArray($sql);
		
		@ob_end_clean();
		
		if(count($rows) > 0) echo '<ul>';
		for($i=0;$i<count($rows);$i++)
		{
			$item = $rows[$i][0] . ' ' . $rows[$i][1];
			echo '<li onClick="fill(\''.addslashes($item).'\');">'.$item.'</li>';
		}
		if(count($rows) > 0) echo '</ul>';
	}
	
	function SuggestUniversity($queryString)
	{
		$queryString = mysql_real_escape_string($queryString);
		$sql = "SELECT DISTINCT u.university FROM userdetail u WHERE u.university LIKE '%$queryString%'
			UNION SELECT DISTINCT l.listData FROM listdata l WHERE l.listType = 'university' AND l.listData LIKE '%$queryString%' LIMIT 10";
		
		CallSuggestion($sql);
	}
?>